package com.controller.system.user;

import com.entity.InviteVo;
import com.entity.system.UserAssets;
import com.entity.system.UserInfo;
import com.entity.system.UserInvite;
import com.service.system.user.UserAdminService;
import com.service.system.user.UserAssetsService;
import com.service.system.user.UserInfoService;
import com.service.system.user.UserInviteService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    UserInfoService userInfoService;

    @Autowired
    UserInviteService userInviteService;

    @Autowired
    UserAdminService userAdminService;

    @Autowired
    UserAssetsService userAssetsService;

    /**
     * 查询用户列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/fundUserList", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> fundUserList(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>();
        map.put("pageSize", request.getParameter("pageSize"));
        map.put("pageNum", request.getParameter("pageNum"));
        if (StringUtils.isNotEmpty(request.getParameter("role"))) {
            map.put("role", request.getParameter("role"));
        }
        if (StringUtils.isNotEmpty(request.getParameter("queryKey"))) {
            //模糊匹配昵称和电话
            map.put("nickName", request.getParameter("queryKey"));
        }
        if (StringUtils.isNotEmpty(request.getParameter("userStatus"))) {
            //用户状态 正常 封停
            map.put("state", request.getParameter("userStatus"));
        }
        return userInfoService.fundUserList(map);
    }

    /**
     * 根据手机号码查找用户
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/findUserByPhone", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> findUserByPhone(HttpServletRequest request) {
        String phone = request.getParameter("phone");
        return userInfoService.findUserByPhone(phone);
    }

    /**
     * 通过昵称或电话查询用户
     *
     * @return
     */
    @RequestMapping(value = "/findUserListByNiceNameOrPhone", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> findUserListByNiceNameOrPhone(HttpServletRequest request) {
        Map<String, String> map = new HashMap<>();
        map.put("pageSize", request.getParameter("pageSize"));
        map.put("pageNum", request.getParameter("pageNum"));
        //通过昵称或电话模糊匹配
        map.put("nickName", request.getParameter("queryKey"));

        return userInfoService.fundUserList(map);
    }

    @RequestMapping(value = "/update/invite", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> updateRoleById(HttpServletRequest request) {
        Map<String, Object> map1 = new HashMap<>();
        String userId = request.getParameter("userId");
        String newUserId = request.getParameter("newUserId");
        if (userId == null || newUserId == null || "NaN".equals(userId) || "NaN".equals(newUserId)) {
            map1.put("status", 0);
            map1.put("msg", "参数为空");
            return map1;

        } else if (newUserId.equals(userId)) {
            map1.put("status", 0);
            map1.put("msg", "邀请人不能是自己");
            return map1;
        }

        //1.删除原有邀请人
        Map<String, Object> map = new HashMap();
//        map.put("userId", userId);
        map.put("newUserId", newUserId);

        userInviteService.deleteUserInviteByUserIdAndNewUserId(map);

        //2.添加新的邀请人
        UserInvite ui = new UserInvite();
        ui.setUserId(Long.valueOf(userId));
        ui.setNewUserId(Long.valueOf(newUserId));
        userInviteService.addUserInvite(ui);
        map1.put("status", 1);
        map1.put("msg", "修改成功");
        return map1;
    }

    @RequestMapping(value = "/invite/search", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> inviteSearch(HttpServletRequest request) {
        Map<String, Object> map1 = new HashMap<>();
        String userId = request.getParameter("userId");

        if (StringUtils.isEmpty(userId) || userId.equals("NaN")) {
            map1.put("status", 0);
            map1.put("msg", "参数错误");
            return map1;
        }
        Map<String, String> map = new HashMap<>();
        map.put("pageSize", request.getParameter("pageSize"));
        map.put("pageNum", request.getParameter("pageNum"));
        map.put("order", "create_time desc");
        map.put("userId", userId);

        //我邀请的人
        Map<String, Object> map2 = userInviteService.selectListUserInvitePage(map);
        List<UserInvite> list = (List<UserInvite>) map2.get("list");

        List<InviteVo> inviteVoList = new ArrayList<>();
        InviteVo iiv;
        for (UserInvite ui : list) {
            iiv = new InviteVo();
            UserInfo user = userAdminService.selectUserByUserId(ui.getNewUserId());
            UserAssets ua = userAssetsService.getUserAssetsByUserId(ui.getNewUserId());
            if (user != null) {
                iiv.setUserId(user.getId());
                iiv.setNickname(user.getNickName());
                iiv.setPhone(user.getPhone());
                iiv.setBirthday(user.getBirthday());
                iiv.setSex(user.getSex());
                iiv.setState(user.getState());
                iiv.setCreateTime(user.getCreateTime());
                iiv.setRole(user.getRole());
                if(ua != null) {
                    iiv.setCashBalance(ua.getCashBalance());
                    if (user.getRole().toUpperCase().equals("USER")) {
                        iiv.setAwardCashBalance(new BigDecimal(0));
                        iiv.setTaskAwardBalance(0);
                    } else {
                        iiv.setAwardCashBalance(ua.getAwardCashBalance());
                        iiv.setTaskAwardBalance(ua.getTaskAwardBalance());
                    }
                    iiv.setAdvertCoin(ua.getAdvertCoin());
                }else {
                    iiv.setCashBalance(new BigDecimal(0));
                    iiv.setAwardCashBalance(new BigDecimal(0));
                    iiv.setTaskAwardBalance(0);
                    iiv.setAdvertCoin(0);
                }
                inviteVoList.add(iiv);
            }
        }

        //邀请我的人
        Map<String, String> map3 = new HashMap<>();
        map3.put("pageSize", "10");
        map3.put("pageNum", "1");
        map3.put("newUserId", userId);
        Map<String, Object> sr2 = userInviteService.selectListUserInvitePage(map3);
        List<UserInvite> list2 = (List<UserInvite>) sr2.get("list");
        if (list2.size() > 0) {
            UserInvite ui = list2.get(0);
            if (ui != null) {
                UserInfo user = userAdminService.selectUserByUserId(ui.getUserId());
                UserAssets ua = userAssetsService.getUserAssetsByUserId(ui.getUserId());
                if (user != null) {
                    InviteVo ivme = new InviteVo();
                    ivme.setUserId(user.getId());
                    ivme.setNickname(user.getNickName());
                    ivme.setPhone(user.getPhone());
                    ivme.setBirthday(user.getBirthday());
                    ivme.setSex(user.getSex());
                    ivme.setState(user.getState());
                    ivme.setCreateTime(user.getCreateTime());
                    ivme.setRole(user.getRole());
                    if (ua != null) {
                        ivme.setCashBalance(ua.getCashBalance());
                        if (user.getRole().toUpperCase().equals("USER")) {
                            ivme.setAwardCashBalance(new BigDecimal(0));
                            ivme.setTaskAwardBalance(0);
                        } else {
                            ivme.setAwardCashBalance(ua.getAwardCashBalance());
                            ivme.setTaskAwardBalance(ua.getTaskAwardBalance());
                        }
                        ivme.setAdvertCoin(ua.getAdvertCoin());
                    } else {
                        ivme.setCashBalance(new BigDecimal(0));
                        ivme.setAwardCashBalance(new BigDecimal(0));
                        ivme.setTaskAwardBalance(0);
                        ivme.setAdvertCoin(0);
                    }
                    map1.put("ivme", ivme);
                }
            }
        }
        map1.put("list", inviteVoList);
        map1.put("page", map2.get("page"));
        map1.put("status", 1);
        return map1;
    }
}
